<?php

namespace App\Http\Controllers\Admin;

use App\Cate;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class CatesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $cates = getCates();
        return view('admin.cate.index', ['cates'=>$cates, 'request'=>$request]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $cates = Cate::get();
        return view('admin.cate.add', ['cates'=>$cates]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->all();
        if($data['pid']==0){
            $data['path'] = '0';
        }else{
            $info = Cate::find($data['pid']);
            $data['path'] = $info->path.','.$info->id;
        }
        $cate = new Cate;
        $cate->name = $data['name'];
        $cate->pid = $data['pid'];
        $cate->path = $data['path'];
        $cate->date = time();
        if($cate->save()){
            return redirect('admin/cate')->with('info', '分类添加成功');
        }else{
            return back()->with('info', '分类添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        echo 'show';
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $cates = getCates();
        $info = Cate::find($id);
        return view('admin.cate.edit', ['cates'=>$cates, 'info'=>$info]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $cates = Cate::find($id);
        $cates->name = $request->input('name');
        $cates->pid = $request->input('pid');
        if($request->input('pid')==0){
            $cates->path = '0';
        }else{
            $info = Cate::find($request->input('pid'));
            $cates->path = $info->path.','.$info->id;
        }
        if($cates->save()){
            return redirect('admin/cate')->with('info', '分类更新成功');
        }else{
            return back()->with('info', '分类更新失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $cate = Cate::findOrFail($id);
        //删除子集分类
        $path = $cate->path.','.$cate->id;
        DB::table('cates')->where('path', 'like', $path.'%')->delete();
        if($cate->delete()){
            return back()->with('info', '分类删除成功');
        }else{
            return back()->with('info', '分类删除失败');
        }
    }
}
